package com.wizz.trade.auth.controller;

import com.wizz.trade.auth.dto.page.SysMenuPageDto;
import com.wizz.trade.auth.entity.SysMenu;
import com.wizz.trade.auth.service.SysMenuService;
import com.wizz.trade.auth.vo.SysMenuVo;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.common.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限模块/权限
 * @author xialinrui
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;



    /**
     * 查询权限
     *
     * @param sysMenuPageDto 查询条件
     * @return 分页信息
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('权限管理')")
    public ResponseResult<PageVo<SysMenuVo>> getPage(SysMenuPageDto sysMenuPageDto) {
        PageVo<SysMenu> rawPage = sysMenuService.getPage(sysMenuPageDto);
        List<SysMenu> data = rawPage.getData();
        List<SysMenuVo> dataVo = new ArrayList<>();
        for(int i = 0;i<data.size();i++){
            SysMenuVo sysMenuVo = new SysMenuVo();
            BeanUtils.copyProperties(data.get(i),sysMenuVo);
            dataVo.add(sysMenuVo);
        }
        return new ResponseResult<>(StatusCode.OK, "查询成功", new PageVo<>(rawPage.getPages(),rawPage.getTotal(), dataVo));
    }

}
